#include "cJSON/cJSON.h"
#include "log/log.h"
#include <stdlib.h>

int main(int argc, char *argv[])
{
    //1.生成json字符串
    cJSON *root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "name", "zhangsan");
    cJSON_AddNumberToObject(root, "age", 18);
    char *json = cJSON_PrintUnformatted(root);
    log_debug("生成json字符串:%s",json);
    //2.解析json字符串
    cJSON *root_json = cJSON_Parse(json);
    if(root_json == NULL){
        log_error("解析json字符串失败");
        return -1;
    }
    char *name1 = cJSON_GetObjectItem(root_json,"name")->valuestring;
    int age1 = cJSON_GetObjectItem(root_json,"age")->valueint;
    log_debug("解析json字符串,name:%s,age:%d",name1,age1);
 
    //3.释放内存
    cJSON_Delete(root);
    cJSON_Delete(root_json);
    free(json);

    return 0;
}